package api

import (
	"batbat-backend/app/model"
	"batbat-backend/app/service"
	"batbat-backend/library/response"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

// Game 游戏API接口
var Game = gameApi{}

type gameApi struct{}

// GetAllGames
// @summary 获取所有游戏
// @tags    游戏
// @produce json
// @success 200 {object} response.JsonResponse{data=[]model.GameResp}
// @router  /game [GET]
func (ga *gameApi) GetAllGames(r *ghttp.Request) {
	games, err := service.Game.GetAllGames(r.Context())
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, games)
}

// Rank
// @summary 获取游戏排行榜
// @tags    游戏
// @param	pn query int false "页码, 默认为1"
// @param	limit query int false "每页显示的个数, 取值范围为15到30, 默认为30"
// @produce json
// @success 200 {object} response.JsonResponse{data=model.GameApiRankResp}
// @router  /game/rank [GET]
func (ga *gameApi) Rank(r *ghttp.Request) {
	var req *model.GameApiRankReq
	err := r.ParseQuery(&req)
	if err != nil {
		response.Error(r, err)
	}
	games, err := service.Game.Rank(r.Context(), req.Pn, req.Limit)
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, games)
}

// Search
// @summary 搜索游戏
// @tags    游戏
// @param	key query string true "搜索关键字, 会从游戏名、分类名、简介中进行搜索"
// @param	pn query int false "页码, 默认为1"
// @param	limit query int false "每页显示的个数, 取值范围为5到10, 默认为10"
// @produce json
// @success 200 {object} response.JsonResponse{data=[]model.GameApiSearchResp}
// @router  /game/search [GET]
func (ga *gameApi) Search(r *ghttp.Request) {
	var req *model.GameApiSearchReq
	err := r.ParseQuery(&req)
	if err != nil {
		response.Error(r, err)
	}
	resp, err := service.Game.Search(r.Context(), req.Key, req.Pn, req.Limit)
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, resp)
}

// Recommend
// @summary 随机获取一些推荐游戏
// @tags 游戏
// @produce json
// @success 200 {object} response.JsonResponse{data=[]model.GameResp}
// @router  /game/recommend [GET]
func (ga *gameApi) Recommend(r *ghttp.Request) {
	var (
		uid  uint = 0
		user *model.UserContext
	)
	err := r.GetCtxVar(model.UserContextKey).Scan(&user)
	if err != nil {
		g.Log().Ctx(r.Context()).Errorf("获取用户上下文错误: %v", err)
		response.Error(r, gerror.New("获取推荐失败"))
	}
	if user != nil {
		uid = user.Id
	}
	games, err := service.Game.Recommend(r.Context(), uid)
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, games)
}

// GetGame
// @summary 获取游戏详情
// @tags    游戏
// @param	id path int true "要获取详情的游戏id"
// @produce json
// @success 200 {object} response.JsonResponse{data=model.GameApiGetGameResp}
// @router  /game/{id} [GET]
func (ga *gameApi) GetGame(r *ghttp.Request) {
	var (
		id   uint     // 游戏id
		uid  uint = 0 // 用户id
		user *model.UserContext
	)
	// 获取要查询的游戏id
	id = r.GetUint("id")
	if id == 0 {
		response.Error(r, gerror.New("路径参数有误, 请检查参数`id`"))
	}
	// 获取用户id
	err := r.GetCtxVar(model.UserContextKey).Scan(&user)
	if err != nil {
		g.Log().Ctx(r.Context()).Errorf("获取用户上下文失败: %v", err)
	}
	if user != nil {
		uid = user.Id
	}
	game, err := service.Game.GetGame(r.Context(), id, uid)
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, game)
}

// Similar
// @summary 获取游戏的三个相似游戏
// @tags    游戏
// @param	id path int true "要获取相似的游戏id"
// @produce json
// @success 200 {object} response.JsonResponse{data=[]model.GameResp}
// @router  /game/{id}/similar [GET]
func (ga *gameApi) Similar(r *ghttp.Request) {
	var id uint // 游戏id
	// 获取要查询的游戏id
	id = r.GetUint("id")
	if id == 0 {
		response.Error(r, gerror.New("路径参数有误, 请检查参数`id`"))
	}
	game, err := service.Game.Similar(r.Context(), id)
	if err != nil {
		response.Error(r, err)
	}
	response.Success(r, game)
}
